package com.liuzhilin.controller;

import com.liuzhilin.domain.User;
import com.liuzhilin.service.UserService;
import com.liuzhilin.util.Response;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
/*
* 用户个人信息查询
* */
@WebServlet("/userInformation")
public class UserInformation extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应类型为 JSON
        Response.setCorsHeaders(response);
        response.setCharacterEncoding("UTF-8");
        // 设置响应内容类型为 JSON
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        // 从请求中获取用户名
        String username = request.getParameter("username");
        username = username.trim();
        // 查询用户信息
        User user = UserService.userInformationSelect(username);
        String status =null;
        if (user != null) {
            System.out.println("用户id：" + user.getStatus());
            status=UserService.userStatus(user.getStatus());
            // 将用户信息转换为 JSON 格式
            String json = "{" +
                    "\"username\": \"" + user.getUserName() + "\"," +
                    "\"personalSignature\": \"" + user.getPersonalSignature() + "\"," +
                    "\"articleCount\": " + user.getArticleCount() + "," +
                    "\"id\": " + user.getId() + "," +
                    "\"birthday\": \"" + user.getBirthday() + "\"," +
                    "\"accountStatus\": \"" + status + "\"" +
                    "}";
            // 返回 JSON 响应
            out.print(json);
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            // 用户不存在，返回错误信息
            out.print("{\"message\": \"用户不存在！\"}");
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    }
    @Override
    public void doOptions(HttpServletRequest request, HttpServletResponse response){
        Response.setCorsHeaders(response);
        response.setStatus(HttpServletResponse.SC_OK);

    }

}
